package dao.hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import vo.StackTraceVo;

/**
 * A data access object (DAO) providing persistence and search support for
 * StackTraceVo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see vo.StackTraceVo
 * @author MyEclipse Persistence Tools
 */
public class StackTraceVoDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(StackTraceVoDAO.class);
	// property constants
	public static final String KEY_CAUSE = "keyCause";
	public static final String ONLINE = "online";
	public static final String STACKTRACE = "stacktrace";

	public void updateKeyCauseUidByUid(final String stackTraceUid ,final String newKeyCauseUid) {
		log.debug("saving StackTraceVo instance");
		String hql = "UPDATE StackTraceVo set keyCause = :newKeyCauseUid "  + 
	             "WHERE uid = :stackTraceUid";
		Transaction tx = null;
		try {
			tx = getSession().beginTransaction();
			Query query = getSession().createQuery(hql);
			query.setParameter("newKeyCauseUid", newKeyCauseUid);
			query.setParameter("stackTraceUid", Integer.valueOf(stackTraceUid));
			int result = query.executeUpdate();
			tx.commit();
			log.debug("update successful");
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			log.error("update failed", e);
		} finally {
			getSession().close();
		}
	}

	public void save(StackTraceVo transientInstance) {
		log.debug("saving StackTraceVo instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	public void delete(StackTraceVo persistentInstance) {
		log.debug("deleting StackTraceVo instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public StackTraceVo findById(java.lang.Integer id) {
		log.debug("getting StackTraceVo instance with id: " + id);
		try {
			StackTraceVo instance = (StackTraceVo) getSession().get(
					"vo.StackTraceVo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(StackTraceVo instance) {
		log.debug("finding StackTraceVo instance by example");
		try {
			List results = getSession().createCriteria("vo.StackTraceVo")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding StackTraceVo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from StackTraceVo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByKeyCause(Object keyCause) {
		return findByProperty(KEY_CAUSE, keyCause);
	}

	public List findByOnline(Object online) {
		return findByProperty(ONLINE, online);
	}

	public List findByStacktrace(Object stacktrace) {
		return findByProperty(STACKTRACE, stacktrace);
	}

	public List<StackTraceVo> findAll() {
		log.debug("finding all StackTraceVo instances");
		try {
			String queryString = "from StackTraceVo";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public StackTraceVo merge(StackTraceVo detachedInstance) {
		log.debug("merging StackTraceVo instance");
		try {
			StackTraceVo result = (StackTraceVo) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(StackTraceVo instance) {
		log.debug("attaching dirty StackTraceVo instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(StackTraceVo instance) {
		log.debug("attaching clean StackTraceVo instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}